var reviewRight ;
var reviewReplyRight ;
var reviewProductId ;



//改变列表排序
function changeOrderBy(){
	$('orderby').form.submit();
}


//--------------------start:投票相关---------------------

//评论投票
function reviewVote(event){
	doRequiredLoginAction(function(){
		$j.post(__ctxPath+'/review/item/blank.html?doAction=reviewVote',{"productReviewId":event.data.productReviewId,"chosen":event.data.chosen},reviewVoteCallback,"json");
	});
}
//评论投票callback
function reviewVoteCallback(data){
	if (data.result == 1){
		$("span_usefulCount_"+data.productReviewId).innerHTML = data.usefulCount;
		$("span_unusefulCount_"+data.productReviewId).innerHTML = data.unusefulCount;
		alert(__FMT.productReview_voteSuccess);
	}else if (data.result == 2){ //已投票
		alert(__FMT.productReview_isVoted);
	}else if (data.result == 3){ //不能投票给自己
		alert(__FMT.productReview_isSelfReview);
	}
}

//--------------------end: 投票相关---------------------


//--------------------start:发表评论相关---------------------
//验证检查
function reviewFormCheck(){
	var flag = true;
	if($('score').value==""){
		alert(__FMT.productReviewForm_rate_required);
		flag = false;
	}
	if($('customerName')!=null){
		applyValidate($('customerName'),"maxlength=20,required,noHtml");
	}
	applyValidate($('subject'),"minlength=3,maxlength=100,required,noHtml");
	applyValidate($('message'),"minlength=10,maxlength=500,required,noHtml");
	if(!validateForm($('addReviewForm'))){
		flag = false;
	}
	return flag;
}

//权限检查 0=匿名可评论 1=登录用户 2=购买过才可评论
function reviewRightCheck(){
	if(reviewRight=='0'){
		return;
	}else if(reviewRight=='1'){
		doRequiredLoginAction(function(){
			return true;
		});
	}else if(reviewRight=='2'){
		doRequiredLoginAction(function(){
			reviewEligibilityCheck();
		});
	}
}

//发表评论检查 0=匿名可评论 1=登录用户 2=购买过才可评论
function addReview(){
	if(reviewRight=='0'){
		submitReview();
	}else if(reviewRight=='1'){
		doRequiredLoginAction(submitReview);
	}else if(reviewRight=='2'){
		doRequiredLoginAction(function(){
			reviewEligibilityCheck(submitReview);
		});
	}
}
//提交评论
function submitReview(){
	if(reviewFormCheck()){
		$('addReviewSubmit').disabled = true;
		$('addReviewForm').submit();
	}
} 

//发表评论资质检查
var globalReviewEligibilityCheckCallback;
function reviewEligibilityCheck(callback) {
	globalReviewEligibilityCheckCallback = callback
	$importDwr(function(){
		reviewEligibilityDwr.getEligibility(reviewProductId,{callback:function (data) {
			if(data == -1){
				//未登录
				doRequiredLoginAction(function(){
					return true;
				});	
				return;
			}else if(data == 0){
				//未购物，不可评论
				alert(__FMT.reviewEligibility_result_0);
				return;
			}else if(data == 2){
				//不可评论，因为已超出可评论次数
				alert(__FMT.reviewEligibility_result_2);
				return;
			}else if(data == 3){
				//不可评论，因为已超出可评论时间
				alert(__FMT.reviewEligibility_result_3);
				return;
			}else if(data == 1){
				//有权评论
				if(globalReviewEligibilityCheckCallback != null){
					globalReviewEligibilityCheckCallback.call(this);
				}	
				return;
			}
			
		}, async:false});
	});
}
//--------------------end:  发表评论相关---------------------


//--------------------start:发表回复相关---------------------
//显示回复窗口
function showReplyForm(event){
	if(reviewReplyRight=='0'){
		showReplyFormGo(event);
	}else if(reviewReplyRight=='1'){
		doRequiredLoginAction(function(){
			showReplyFormGo(event);
		});	
	}
		
}

function showReplyFormGo(event){
	if(!$j("#replyDiv_"+event.data.productReviewId).hasClass("hasLoad")){
		$j("#replyDiv_"+event.data.productReviewId).loading().load(__ctxPath+'/review/item/blank.html?doAction=showReplyForm',{"productReviewId":event.data.productReviewId});
		$j("#replyDiv_"+event.data.productReviewId).addClass("hasLoad");
	}
}

//关闭回复窗口
function closeReview(productReviewId,buttonDOM){
	$j("#replyDiv_"+productReviewId).empty();
	$j("#replyDiv_"+productReviewId).removeClass("hasLoad");
}



//发表回复
function replyReview(productReviewId,buttonDOM){
	if(reviewReplyRight=='0'){
		submitReply(productReviewId,buttonDOM)
	}else if(reviewReplyRight=='1'){
		doRequiredLoginAction(function(){
			submitReply(productReviewId,buttonDOM);
		});	
	}
	
}
//提交回复
function submitReply(productReviewId,buttonDOM){
	if(replyFormCheck(productReviewId)){
		var postData = $j("#replyForm_"+productReviewId+" :input").serializeArray();
		$j.post(__ctxPath+'/review/item/blank.html?doAction=replyReview',postData,submitReplyCallback,"json");
		buttonDOM.disabled = true;
	}	
}

//提交回复callback
function submitReplyCallback(data){
	alert(__FMT.productReviewReplyForm_add_success);
	window.location.reload(); 

}

//回复表单检查
function replyFormCheck(productReviewId){
	var flag = true;
	applyValidate($('replyMessage_'+productReviewId),"minlength=10,maxlength=500,required");
	if($('replyCustomerName_'+productReviewId)!=undefined){
		applyValidate($('replyCustomerName_'+productReviewId),"maxlength=20,required");
	}	
	if(!validateForm($('replyForm_'+productReviewId))){
		flag = false;
	}
	return flag;
}
//--------------------end:  发表回复相关---------------------





